package com.example.huawei.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.huawei.system.entity.LikeRecord;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.LikeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "点赞相关接口")
@RestController
@RequestMapping("/like")
public class LikeRecordController {

    @Autowired
    private LikeRecordService likeRecordService;

    @ApiOperation(value = "点赞/取消点赞", notes = "点赞/取消点赞")
    @PostMapping("/likeCancelLike")
    public Result<?> likeCancelLike(@RequestBody LikeRecord likeRecord) {

        LikeRecord one = likeRecordService.getOne(new LambdaQueryWrapper<LikeRecord>()
                .eq(LikeRecord::getUserId, likeRecord.getUserId())
                .eq(LikeRecord::getReleaseId, likeRecord.getReleaseId())
        );
        boolean flag;
        if (one == null) {
            LikeRecord entity = new LikeRecord();
            entity.setLikeFlag(1);
            entity.setUserId(likeRecord.getUserId());
            entity.setReleaseId(likeRecord.getReleaseId());
            entity.setCreateTime(new Date());
            flag = likeRecordService.save(entity);
        } else {
            flag = likeRecordService.removeById(one);
        }

        return flag ? Result.ok() : Result.error();
    }

}
